package com.ruiysoft.controller.integralmdse;

import com.alibaba.fastjson.JSONObject;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruiysoft.controller.integralmdse.util.IntegralMdseUtil;
import com.ruiysoft.controller.integralmdse.vo.IntegralMdseSaveVo;
import com.ruiysoft.entity.integralmdse.MerchandiseSkuMPRela;
import com.ruiysoft.entity.sys.SysMicroShop;
import com.ruiysoft.mapper.integralmdse.MerchandiseSkuMPRelaMapper;
import com.ruiysoft.pub.core.Result;
import com.ruiysoft.pub.core.ResultGenerator;
import com.ruiysoft.service.integralmdse.IMerchandiseSkuMPRelaService;
import com.ruiysoft.service.sys.clent.LoginContextUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2019-04-27T03:44:58.202+08:00")

@RestController
public class IntegralMdseApiController implements IntegralMdseApi {

    private static final Logger log = LoggerFactory.getLogger(IntegralMdseApiController.class);

    private final IMerchandiseSkuMPRelaService merchandiseSkuMPRelaService;

    @org.springframework.beans.factory.annotation.Autowired
    public IntegralMdseApiController(IMerchandiseSkuMPRelaService merchandiseSkuMPRelaService) {
        this.merchandiseSkuMPRelaService = merchandiseSkuMPRelaService;
    }

    @Autowired
    MerchandiseSkuMPRelaMapper merchandiseSkuMPRelaMapper;

    private Long getShopId() {
        SysMicroShop shop = LoginContextUtil.instance().getEntryMicroShop();
        return shop.getId();
    }

    @Override
    public Result<Map<Long, IntegralMdseSaveVo>> integralMdseBatchPost(@ApiParam(value = ""  )  @Valid @RequestBody  List<Long> skuIds) {
        Long shopId = getShopId();
        try{
            List<MerchandiseSkuMPRela> list = merchandiseSkuMPRelaService.list(new QueryWrapper<MerchandiseSkuMPRela>().in("sku_id",skuIds));
//            Map<Long, IntegralMdseSaveVo> map = list.stream().collect(Collectors.groupingBy(a->IntegralMdseUtil.BListToVo(list)));
//            Map<Long, List<MerchandiseSkuMPRela>> map =list.stream().collect(Collectors.groupingBy(a->a.getSkuId(),Collectors.toList()));
            Map<Long, IntegralMdseSaveVo> map = list.stream().collect(Collectors.groupingBy(a->a.getSkuId(),Collectors.toList()))
                    .entrySet().stream().collect(Collectors.toMap(a->a.getKey(),a->IntegralMdseUtil.BListToVo(a.getValue())));
            return ResultGenerator.genSuccessResult(map);
        }catch (Exception e){
            log.error("商铺批量查询sku会员积分设置--{}--{}",shopId,JSONObject.toJSONString(skuIds), e);
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }

    @Override
    public Result<Void> integralMdseSavePost(@ApiParam(value = ""  )  @Valid @RequestBody IntegralMdseSaveVo body) {
        Long shopId = getShopId();
        log.info("商品会员与积分设置---{}",JSONObject.toJSONString(body));
        Objects.requireNonNull(body.getSkuId(),"skuId不能为空");
        List<Long> skuIds = new ArrayList<>();
        skuIds.add(body.getSkuId());
        merchandiseSkuMPRelaService.save(IntegralMdseUtil.voToBList(body,shopId), skuIds);
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Result<Void> integralMdseSavebatchPost(@ApiParam(value = ""  )  @Valid @RequestBody  List<IntegralMdseSaveVo> list) {
        Long shopId = getShopId();

        log.info("商品会员与积分设置---{}",JSONObject.toJSONString(list));
        List<Long> skuId = list.stream().map(a->a.getSkuId()).distinct().collect(Collectors.toList());

        IntegralMdseSaveVo integralMdseSaveVo = list.get(0);
        if(integralMdseSaveVo != null) {
            if(integralMdseSaveVo.getType().equals(4)) {
                int rtn = merchandiseSkuMPRelaMapper.delete(new QueryWrapper<MerchandiseSkuMPRela>().eq("shop_id", shopId).eq("sku_id", integralMdseSaveVo.getSkuId()));
                if(rtn > 0)
                    return ResultGenerator.genSuccessResult();
                else
                    return ResultGenerator.genFailResult("操作失败");
            }
        }

        merchandiseSkuMPRelaService.save(IntegralMdseUtil.voListToBList(list,shopId),skuId);
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Result<IntegralMdseSaveVo> integralMdseSkuIdGet(@ApiParam(value = "skuId",required=true) @PathVariable("skuId") Long skuId) {
        Long shopId = getShopId();
        try{
            List<MerchandiseSkuMPRela> list = merchandiseSkuMPRelaService.list(new QueryWrapper<MerchandiseSkuMPRela>().eq("sku_id",skuId));
            return ResultGenerator.genSuccessResult(IntegralMdseUtil.BListToVo(list));
        }catch (Exception e){
            log.error("商铺查询sku会员积分设置--{}--{}",shopId,skuId, e);
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }

}
